/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.core.scap.cpe.dictionary;

import jp.go.aist.six.util.castor.CastorDao;
import jp.go.aist.six.vuln.model.scap.cpe.dictionary.CpeItem;
import jp.go.aist.six.vuln.model.scap.cpe.dictionary.CpeTitle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



/**
 * @author  Akihito Nakamura, AIST
 * @version $Id: CpeItemDao.java 256 2010-06-14 03:58:05Z nakamura5akihito $
 */
public class CpeItemDao
    extends CastorDao<CpeItem>
{

    /**
     * Logger.
     */
    private static Log  _LOG = LogFactory.getLog( CpeItemDao.class );



    /**
     * Constructor.
     */
    public CpeItemDao()
    {
        super( CpeItem.class, new CpeItemHelper() );
    }



    private void _prepare(
                    final CpeItem item
                    )
    {
        for (CpeTitle  title : item.getTitle()) {
            title.setMasterObject( item );
        }
    }



    //**************************************************************
    //  Dao, CastorDao
    //**************************************************************

    @Override
    public Object create(
                    final CpeItem item
                    )
    {
        if (_LOG.isInfoEnabled()) {
            _LOG.info( "create: id=" + item.getName() );
        }

        _prepare( item );

        return super.create( item );
    }



    @Override
    public CpeItem sync(
                    final CpeItem item
                    )
    {
        if (_LOG.isInfoEnabled()) {
            _LOG.info( "sync: " + item.getName() );
        }
        CpeItem  p_item = super.sync( item );

        if (p_item != item) {
            if (_LOG.isTraceEnabled()) {
                _LOG.trace( "CPE item already exists: name=" + item.getName() );
            }
            // item is newer than p_item
            p_item.setTitle( item.getTitle() );
            _prepare( p_item );
        }

        return p_item;
    }

}
// CpeItemDao
